package controler;
import java.util.Calendar;
import java.util.Date;



public class Item{
	private String Name;
	private Double MaxPrice;
	private String Url;
	private Date FinishTime;
    private IProvider provider;
    private Credentials credentials;
    private Boolean isRunning=false;
    public Item () { }
    public Item (String Url, Credentials c) { }

    public Double getMaxPrice () {
        return this.MaxPrice;
    }

    public Boolean isRuninng(){
    	return this.isRunning;
    }
    
    public void setMaxPrice (Double val) {
        this.MaxPrice = val;
    }

    public String getName () {
        return Name;
    }

    private void setName (String val) {
        this.Name = val;
    }

    public String getUrl () {
        return Url;
    }

    public void setUrl (String val) {
        this.Url = val;
    }
    
    private void setFinishTime(Date val) {
        this.FinishTime = val;
    }
 
    public Date GetFinishTime () {
        return FinishTime;
    }
 
    public Boolean Buy () {
        return provider.Buy(this, credentials);
    }

    public Float GetCurrentPrice () {
        return provider.GetPrice(this);
    }

    public void setCredentials(Credentials c){
    	this.credentials =c;
    }
    
    public void setCredentials(String username, String password){
    	Credentials c = new Credentials(username,password);
    	this.setCredentials(c);
    }
    
    public Item (Credentials c, String url) throws ItemException {
        try {
            this.provider = null;
            this.provider = RecognizeProvider(url);
            this.setUrl(url);
            this.setName(this.provider.GetName(this));
            this.setFinishTime(this.provider.GetFinishDate(this));
            this.setCredentials(c);
            
        }catch (ItemException ex) {
          //  Logger.getLogger(Item.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }

    public Item(String url2, String username, String password) throws ItemException {
		this(new Credentials(username,password),url2);
		
	}
	public final IProvider RecognizeProvider (String url) throws Exception {
        Providers recognizedProvider=Providers.Unknown;
        IProvider p;
        if(url.contains("allegro.pl")) recognizedProvider=Providers.Allegro;
        else if(url.startsWith("http://www.ebay")) recognizedProvider=Providers.Ebay;
        else if(url.startsWith("http://www.swistak.pl")) recognizedProvider=Providers.Swistak;
        else recognizedProvider=Providers.Unknown;
        
        switch(recognizedProvider){
            case Allegro:
                p= new Allegro();
                break;
            case Ebay:
                p = new Ebay();
                break;
            case Swistak:
                p= new Swistak();
                break;
            default: 
                Exception e = new Exception("Error: Cannot recognize provider");
                throw e;
        }
        return p;       
    }
	
	Runnable ltm = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			isRunning=true;
			Calendar c1 = Calendar.getInstance();
			c1.setTime(GetFinishTime());
			Calendar now = Calendar.getInstance();
			
			long dateDiff = c1.getTimeInMillis() - now.getTimeInMillis();
			
			System.out.println("Reamining "+ (dateDiff /1000) + " seconds");
			
			try {
				Thread.sleep(dateDiff - 60000);
				provider.Login(credentials);
				Thread.sleep(55000);
				System.out.println(getName() + " - 5 secs remaning. I Buy it!");
				System.out.println("end: " + c1.getTime() + " current:" + Calendar.getInstance().getTime());
				Buy();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (LoginException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		isRunning = false;	
		}
		
	};
	
	public void Login() throws LoginException{
		provider.Login(credentials);
	}


    

}

